
package com.jf.cloud.user.listener;

import com.jf.cloud.api.user.bo.BuyVipNotifyBO;
import com.jf.cloud.common.cache.constant.UserCacheNames;
import com.jf.cloud.common.cache.util.CacheManagerUtil;
import com.jf.cloud.common.rocketmq.config.RocketMqConstant;
import com.jf.cloud.common.util.Json;
import com.jf.cloud.user.model.UserLevelLog;
import com.jf.cloud.user.service.UserLevelLogService;
import com.jf.cloud.user.service.UserLevelService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zz
 * @date 2021/1/7
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.BUY_VIP_NOTIFY_TOPIC,consumerGroup = RocketMqConstant.BUY_VIP_NOTIFY_TOPIC)
public class BuyVipNotifyConsumer implements RocketMQListener<BuyVipNotifyBO> {

    @Autowired
    private UserLevelLogService userLevelLogService;
    @Autowired
    private UserLevelService userLevelService;
    @Autowired
    private CacheManagerUtil cacheManagerUtil;

    private static final Logger LOG = LoggerFactory.getLogger(BuyVipNotifyConsumer.class);

    @Override
    public void onMessage(BuyVipNotifyBO message) {
        LOG.info("购买vip回调开始... message: " + Json.toJsonString(message));
        UserLevelLog userLevelLog = userLevelLogService.getByLevelLogId(message.getUserLevelLogId());

        userLevelService.paySuccess(userLevelLog, message);
        // 移除用户缓存
        cacheManagerUtil.evictCache(UserCacheNames.USER_INFO, userLevelLog.getUserId().toString());
    }
}
